//
//  mainMy.m
//  HappyStore
//
//  Created by 点点充 on 16/1/14.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "mainMy.h"
//#import "mainHeadView.h"
#import "myDetailVC.h"
#import "goveDetailVC.h"
#import "My_ReleaseChange.h"
#import "downFile.h"
#import "mainMyCell.h"
#import "HeadView.h"
#import "moreSetting.h"
#import "LSMyPuerseVC.h"
#import "LSBecomeVIPVC.h"
#import "MySetting.h"
@interface mainMy ()
{
    NSArray *aryTitle;
    NSArray *aryImgName;
    NSDictionary *dicData;
    
    NSInteger ReceiveResumeType;
    UIButton * headBtn;
}
//@property (strong, nonatomic) mainHeadView * headView;
@property (strong, nonatomic) HeadView *headView;
@property (strong, nonatomic) NSString *levelovertime;//会员到期时间

//数据模型
@property (strong, nonatomic)Person * person;
@end

@implementation mainMy

- (HeadView *)headView{
    if (!_headView) {
        _headView = [[[NSBundle mainBundle] loadNibNamed:@"HeadView" owner:self options:nil] lastObject];
    }
    return _headView;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.hidden = NO;
//    self.navigationController.navigationBarHidden = YES;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16],NSFontAttributeName,[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
//    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:67.0/255.0 green:177.0/255.0 blue:241.0/255.0 alpha:1]];

    //找出导航栏的下边的黑线
    UIImageView *navBarHairlineImageView = [NaviBarLine findHairlineImageViewUnder:self.navigationController.navigationBar];
    navBarHairlineImageView.hidden = NO;

}

- (void)viewDidLoad {
    [super viewDidLoad];

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.text = @"我的";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
    // 声明这张图片用原图(别渲染)
    UIImage *selectedImgitemHome = [UIImage imageNamed:@"btn_wd_click"];
    selectedImgitemHome = [selectedImgitemHome imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.navigationController.tabBarItem setSelectedImage:selectedImgitemHome];

    if ([_person.vipType integerValue] != 1) {
        
        aryTitle = @[@"我的发布",@"我的关注",@"我的粉丝",@"我的引荐",@"我的钱包",@"更多"];
        
        aryImgName = @[@"我的-我的发布",@"我的-我的关注",@"我的-我的粉丝",@"我的-我的引荐",@"我的-我的钱包",@"我的-更多"];
    
    }else{
    
        aryTitle = @[@"我的发布",@"我的关注",@"我的粉丝",@"我的引荐"];
        
        aryImgName = @[@"我的-我的发布",@"我的-我的关注",@"我的-我的粉丝",@"我的-我的引荐"];
    
    }
    
    //初始化数据
    
    Person * person = [Person MR_findFirstByAttribute:@"userid" withValue:allConstMemberid];
    _person = person;
    [self startPostNetWork];

}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"%@",_person.vipType);
     if ([_person.vipType integerValue] != 1 ) {
        
        return 3;
        
    }else{
        
    return 2;
        
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return aryImgName.count;
    }else if (section == 1){
        return 1;
    }else if (section == 2){
        return 1;
    }else return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 53;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    mainMyCell *cell = [[mainMyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mainCell"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
     if (indexPath.section ==0){
   
         cell.imageView.image = [UIImage imageNamed:aryImgName[indexPath.row]];
         cell.textLabel.text = aryTitle[indexPath.row];
    }
    
     if ([_person.vipType integerValue] != 1) {
    
         if(indexPath.section == 1){
            
            
            cell.imageView.image = [UIImage imageNamed:@"我的-购买会员"];
            cell.textLabel.text = @"购买会员";
            
        }else if (indexPath.section == 2){
            
            
            cell.imageView.image = [UIImage imageNamed:@"我的-设置"];
            cell.textLabel.text = @"设置";
        }

    
    }else{
    
        if(indexPath.section == 1){
            
            cell.imageView.image = [UIImage imageNamed:@"我的-设置"];
            cell.textLabel.text = @"设置";
            
        }
    
    }
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//取消选中状态
    self.tabBarController.tabBar.hidden = YES;
    
    //取消显示后退键文字
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barButtonItem;

    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:{//我的发布
                My_ReleaseChange *releaseVC = [[My_ReleaseChange alloc] init];
                [self.navigationController pushViewController:releaseVC animated:YES];
            }
                break;
            case 1:{//我的关注
                MyFocus *myfocus = [self.storyboard instantiateViewControllerWithIdentifier:@"myfocus"];
                [self.navigationController pushViewController:myfocus animated:YES];
            }
                break;
            case 2:{//我的粉丝
                [self btnClickLove];
            }
                break;
            case 3:{//我的引荐
                UIViewController *pingtai = [self.storyboard instantiateViewControllerWithIdentifier:@"myyinjian"];
                [self.navigationController pushViewController:pingtai animated:YES];
            }
                break;
                
            case 4:{
                LSMyPuerseVC * LSMyPuerse = [[LSMyPuerseVC alloc]init];
                [self.navigationController pushViewController:LSMyPuerse animated:YES];
            }
                break;
                
            case 5:{//更多
                moreSetting *moreSetVC = [[moreSetting alloc] init];
                moreSetVC.authStateString = _person.authState;
                moreSetVC.levelovertime = self.levelovertime;
                [self.navigationController pushViewController:moreSetVC animated:YES];
            }
                break;
            default:
                break;
        }
    }
    
     if ([_person.vipType integerValue] != 1) {
    
         if (indexPath.section == 1){//会员
            
            LSBecomeVIPVC * LSBecomeVIP = [[LSBecomeVIPVC alloc]init];
            [self.navigationController pushViewController:LSBecomeVIP animated:YES];
            
        }else if (indexPath.section == 2){
            MySetting *settingVC = [[MySetting alloc] init];
            [self.navigationController pushViewController:settingVC animated:YES];
            //        [self btnClickSetting];
            
        }

    
    }else{
    
        if (indexPath.section == 1){
            
            MySetting *settingVC = [[MySetting alloc] init];
            [self.navigationController pushViewController:settingVC animated:YES];
            //        [self btnClickSetting];
            
        }
    
    }
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"myauthuser"]) {//实名认证
        authUser *result = segue.destinationViewController;
        result.strHeaderURl = dicData[@"headPhoto"];
        if (dicData[@"userName"] == [NSNull null] || [dicData[@"userName"] isEqualToString:@"null"]) {
            result.strUserName = _person.account;
        }else {
            result.strUserName = _person.userName;
        }
    }else if ([segue.identifier isEqualToString:@"pushGerenDetail"]) {//详情－个人
        My_Detail *detail = segue.destinationViewController;
        detail.dicInfo = [NSMutableDictionary dictionaryWithDictionary:dicData];
    }else if ([segue.identifier isEqualToString:@"myqiyedetail"]) {//详情－企业
        Qiye_Detail *detail = segue.destinationViewController;
        detail.dicInfo = [NSMutableDictionary dictionaryWithDictionary:dicData];
    }
    else if ([segue.identifier isEqualToString:@"pushReceiveResume"]) {//投递、邀请
        ReceiveResume *receive = segue.destinationViewController;
        receive.mainType = ReceiveResumeType;
    }
    else if ([segue.identifier isEqualToString:@"pushGovernmentDetail"]) {//详情－政府
        Government_Detail *government = segue.destinationViewController;
        government.dicInfo = [NSMutableDictionary dictionaryWithDictionary:dicData];
    }
    
    
}

#pragma mark - 初始化数据
- (void)startPostNetWork
{

   [self.headView setInit:_person];
    self.levelovertime = _person.levelLovertime;

    [self.mainTableView reloadData];
    
    [self.view layoutIfNeeded];
    
    headBtn = [[UIButton alloc]init];
    [headBtn setImage:[UIImage imageNamed:@"youjiantou"] forState:UIControlStateNormal];
    headBtn.imageEdgeInsets = UIEdgeInsetsMake(0, screenW-58, 0, 0);
    headBtn.frame = CGRectMake(self.headView.imgStr.x, self.headView.imgStr.y, screenW, self.headView.imgStr.height);
    [self.mainTableView addSubview:headBtn];
    [headBtn addTarget:self action:@selector(goSetUp) forControlEvents:UIControlEventTouchUpInside];
    
    self.mainTableView.tableHeaderView = self.headView;
            
}

-(void)goSetUp{
    //个人信息
    if ([allConstMemberUserType integerValue] ==1 ) {//个人
        //        [self performSegueWithIdentifier:@"pushGerenDetail" sender:self];
        myDetailVC * detailVC = [[myDetailVC alloc]init];
        detailVC.dicData = [dicData copy];
        [self.navigationController pushViewController:detailVC animated:YES];
    }else if([allConstMemberUserType integerValue] == 2 ){//企业
        //        [self performSegueWithIdentifier:@"myqiyedetail" sender:self];
        myDetailVC * detailVC = [[myDetailVC alloc]init];
        detailVC.dicData = [dicData copy];
        [self.navigationController pushViewController:detailVC animated:YES];
    }else {
        goveDetailVC * govedetailVC = [[goveDetailVC alloc]init];
        //        govedetailVC.addressStr = dicData[@"username"];
        govedetailVC.userId = allConstMemberid;
        govedetailVC.userType = dicData[@"userType"];
        govedetailVC.isFlag = YES;
        self.tabBarController.tabBar.hidden = YES;
        govedetailVC.view.backgroundColor = [UIColor whiteColor];
        [self.navigationController pushViewController:govedetailVC animated:YES];
        //        [self performSegueWithIdentifier:@"pushGovernmentDetail" sender:self];
    }
}
#pragma mark - 设置
- (void)btnClickSetting
{
    //取消显示后退键文字
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barButtonItem;
    
    My_Setting *sett = [self.storyboard instantiateViewControllerWithIdentifier:@"mysetting"];
    sett.delegate = self;
    sett.dicData = dicData;
    [self.navigationController pushViewController:sett animated:YES];
    
}

#pragma mark - 注销
- (void)dismissControll
{
    self.tabBarController.selectedIndex = 0;
}


#pragma mark - 我的粉丝
- (void)btnClickLove
{
    //取消显示后退键文字
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barButtonItem;
    
    [self performSegueWithIdentifier:@"myfensi" sender:self];
}

@end
